/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystemInfo;

import java.awt.Desktop;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import tcloudclient.fileSystem.FileNode;

/**
 *
 * @author Tomas
 */
public class FileSystemInfoPanel extends javax.swing.JPanel {

    private FileSystemTreeNode treeRootNode;
    private FileNode selectedTableNode;
    private FileSystemTreeNode selectedTreeNode;

    /**
     * Creates new form FileSystemInfoPanel
     */
    public FileSystemInfoPanel() {
        initComponents();
        jTree2.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree2.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                TreePath leadSelectionPath = e.getNewLeadSelectionPath();
                if(leadSelectionPath == null){
                    leadSelectionPath = e.getOldLeadSelectionPath();
                }
                selectedTreeNode = (FileSystemTreeNode) leadSelectionPath.getLastPathComponent();
                setTableData(selectedTreeNode.getUserObject());
            }
        });
        
        jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int selectedRow = jTable1.getSelectedRow();
                if(selectedRow >= 0){
                    selectedTableNode = selectedTreeNode.getUserObject().getChilds().get(selectedRow);
                }
            }
        });
    }

    private FileSystemTreeNode createNodes(FileNode fileNode) {
        FileSystemTreeNode root = new FileSystemTreeNode(fileNode);
        addChilds(root, fileNode.getChilds());
        return root;
    }

    private void addChilds(FileSystemTreeNode root, List<FileNode> childs) {
        for (FileNode fileNode : childs) {
            if (!fileNode.isLeaf()) {
                FileSystemTreeNode child = new FileSystemTreeNode(fileNode);
                addChilds(child, fileNode.getChilds());
                root.add(child);
            }
        }
    }

    public void setRootData(FileNode rootNode) {
        treeRootNode = createNodes(rootNode);
        jTree2.setModel(new DefaultTreeModel(treeRootNode, true));
        if (selectedTreeNode == null) {
            selectedTreeNode = treeRootNode;
        }
        setTableData(selectedTreeNode.getUserObject());
    }

    private void setTableData(FileNode fileNode) {
        jTable1.setModel(new FileSystemTableModel(fileNode));
        TableColumn column = jTable1.getColumnModel().getColumn(0);

        column.setWidth(20);
        column.setMinWidth(20);
        column.setMaxWidth(20);
        column.setPreferredWidth(20);
        column.setResizable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonOpen = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree2 = new javax.swing.JTree();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonOpen.setText("Otevřít");
        jButtonOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOpenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButtonOpen))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButtonOpen))
        );

        jSplitPane1.setDividerLocation(150);

        jScrollPane2.setViewportView(jTree2);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable1);

        jSplitPane1.setRightComponent(jScrollPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 909, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 589, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenActionPerformed
        try {
            // TODO add your handling code here:
            Desktop.getDesktop().browse(selectedTableNode.getFile().toURI());
        } catch (IOException ex) {
            Logger.getLogger(FileSystemInfoPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jButtonOpenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOpen;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTree jTree2;
    // End of variables declaration//GEN-END:variables
}
